package com.uphone.magicleafteacher.view.calendar;

import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.radish.baselibrary.dialog.RadishDialog;
import com.radish.baselibrary.utils.LogUtils;
import com.uphone.magicleafteacher.R;
import com.uphone.magicleafteacher.util.DateUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class CalendarDialog {

    private static RadishDialog mCalendarDialog;
    private static TextView tvDate;
    private static Handler mHandler = new Handler();
    private static CalendarAdapter mAdapter;

    public static void dismiss() {
        if (mCalendarDialog != null) {
            mCalendarDialog.dismiss();
        }
    }

    public static void release() {
        if (mCalendarDialog != null) {
            mCalendarDialog.dismiss();
            mCalendarDialog = null;
            mAdapter = null;
        }
    }

    public interface OnCalendarCallback {
        void select(int year, int mouth, int day);
    }

    public static void show(Context context, OnCalendarCallback callback) {
        if (mCalendarDialog == null) {
            mCalendarDialog = new RadishDialog.Builder(context)
                    .setView(R.layout.dialog_calendar)
                    .setFullWidth()
                    .setCanceledOnTouchOutside(false)
                    .setClick(R.id.dialog_cancel, (dialog, view) -> {
                        dialog.dismiss();
                    })
                    .setClick(R.id.dialog_submit, (dialog, view) -> {
                        if (!mAdapter.isTodayAfter()) {
                            ToastUtils.showShort("归还时间必须在今天之后");
                            return;
                        }
                        dialog.dismiss();
                        if (callback != null)
                            callback.select(mAdapter.getYear(), mAdapter.getMonth(), mAdapter.getSelectDay());
                    })
                    .setClick(R.id.dialog_previous, (dialog, view) -> {
                        if (mAdapter.getMonth() == 1) {
                            mAdapter.setYear2Month(mAdapter.getYear() - 1, 12);
                        } else {
                            mAdapter.setMonth(mAdapter.getMonth() - 1);
                        }
                        tvDate.setText(mAdapter.getMonthFormat());
                        loadCalendar();
                    })
                    .setClick(R.id.dialog_next, (dialog, view) -> {
                        if (mAdapter.getMonth() == 12) {
                            mAdapter.setYear2Month(mAdapter.getYear() + 1, 1);
                        } else {
                            mAdapter.setMonth(mAdapter.getMonth() + 1);
                        }
                        tvDate.setText(mAdapter.getMonthFormat());
                        loadCalendar();
                    }).show();

            RecyclerView recyclerView = mCalendarDialog.findView(R.id.dialog_recyclerView);
            tvDate = mCalendarDialog.findView(R.id.dialog_message);

            Calendar c = Calendar.getInstance();
            int day = c.get(Calendar.DAY_OF_MONTH);
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH) + 1;

            recyclerView.setLayoutManager(new GridLayoutManager(context, 7));
            mAdapter = new CalendarAdapter(year, month, day);
            recyclerView.setAdapter(mAdapter);
            mAdapter.setOnItemClickListener((adapter, view, position) -> {
                mAdapter.setSelectPosition(position);
                itemClick(position);
            });
            tvDate.setText(mAdapter.getMonthFormat());
            loadCalendar();
        }
        if (!mCalendarDialog.isShowing())
            mCalendarDialog.show();
    }

    private static void loadCalendar() {
        new Thread() {
            @Override
            public void run() {
                super.run();
                List<DayBean> calendarList = new ArrayList();
                // 获取到的dayForWeek是以星期日为第一天的，所以如果想以星期一为第一天需要 -1；
                int dayOne = DateUtils.dayForWeek(mAdapter.getDateFormat()) - 1;
                if (dayOne == 0)
                    dayOne = 7;
                int lastMonth = 0;
                if (mAdapter.getMonth() == 1) {
                    lastMonth = 12;
                } else {
                    lastMonth = mAdapter.getMonth() - 1;
                }
                int lastMonthDays = DateUtils.getMonthDays(mAdapter.getYear(), lastMonth);
                for (int i = 1; i < dayOne; i++) {
                    DayBean day = new DayBean(lastMonthDays - dayOne + i + 1, -1);
                    calendarList.add(day);
                }
                int thisMonthDays = DateUtils.getMonthDays(mAdapter.getYear(), mAdapter.getMonth());
                for (int i = 1; i <= thisMonthDays; i++) {
                    DayBean day = new DayBean(i, 0);
                    calendarList.add(day);
                }
                int wholeDays = 7 * 6;
                int endThisMonthDays = (dayOne - 1) + thisMonthDays;//到月底一共有多少天
                int afterDays = wholeDays - endThisMonthDays;//还剩多少天

                for (int i = 1; i <= afterDays; i++) {
                    DayBean day = new DayBean(i, -2);
                    calendarList.add(day);
                }
                LogUtils.e("日历day:" + new Gson().toJson(calendarList));
                mHandler.post(() -> {
                    mAdapter.setNewData(calendarList);
                    itemClick(mAdapter.getSelectPosition());
                });
            }
        }.start();
    }

    private static void itemClick(int position) {

    }
}
